﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MahApps.Metro.Controls;

namespace BTM_Client_Application.DeviceViewer
{
    /// <summary>
    /// Interaction logic for DeviceControlWindow.xaml
    /// </summary>
    public partial class DeviceControlWindow
    {

        private ObservableCollection<PanoramaGroup> Devices { get; set; }
        private Dictionary<String, ObservableCollection<DeviceControlModel>> _devices;

        public DeviceControlWindow()
        {
            InitializeComponent();
        }

        public void AddDevice(string deviceTitle, string station, UserControl deviceControl)
        {
            if (!_devices.ContainsKey(deviceTitle))
            {
                _devices.Add(deviceTitle, new ObservableCollection<DeviceControlModel>());
                //New panorama group
                var dev = new PanoramaGroup(deviceTitle);
                //Set source for panorama group
                dev.SetSource(_devices[deviceTitle]);
                //Append Group to control
                Devices.Add(dev);
            }

            //var dcc = new DeviceControlContainer(deviceControl);
            //_models.Add(new DeviceControlModel() { Station = station, ControlContainer = dcc });
            
            /*
            var t = new Tile {
                Content = dcc, 
                Style = (Style) FindResource("TileStyle1"), 
                Title = station};
            
            t.MouseDoubleClick += TileOnMouseDoubleClick;
            _devToShow.Add(t);
            */
            Activate();
            try
            {
                var some = _devices[deviceTitle].First(x => x.Station == station);
            }
            catch (Exception)
            {
                _devices[deviceTitle].Add(new DeviceControlModel { Station = station, ControlContainer = deviceControl });
            }
            
            
            //_devices[deviceTitle].Add(deviceControl);
        }

        private void TileOnMouseDoubleClick(object sender, MouseButtonEventArgs mouseButtonEventArgs)
        {
            var t = sender as Tile;
        }

        public void RemoveDevice(string deviceTitle, string station)
        {
            var toRemove = _devices[deviceTitle].Where(x => x.Station == station);
            foreach (var tile in toRemove)
            {
                _devices[deviceTitle].Remove(tile);
            }
        }

        private void DeviceControlWindow_OnInitialized(object sender, EventArgs e)
        {
            DevicePanorama.ItemBox = 140;
            Devices = new ObservableCollection<PanoramaGroup>();
            _devices = new Dictionary<string, ObservableCollection<DeviceControlModel>>();
            DevicePanorama.ItemsSource = Devices;
        }
    }
}
